package basis.第十四章;


import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;

class TaskWithResult implements Callable<String>{
    private Integer id;

    public TaskWithResult(Integer id) {
        this.id = id;
    }


    @Override
    public String call() throws Exception {
        System.out.println(id);
        return "result is"+id;
    }
}
public class Example2124 {
    public static void main(String[] args){
        ExecutorService executorService = Executors.newCachedThreadPool();

        List<Future<String>> futureList = new ArrayList<>();
        for(int i=0;i<10000;i++){
            Future<String> submit = executorService.submit(new TaskWithResult(i));
            futureList.add(submit);
        }

        for(Future<String> fs :futureList){
            try {
                System.out.println(fs.get());
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }finally {
                executorService.shutdown();
            }
        }
    }
}
